package br.cin.ufpe.corba.iiop;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class RequestHeader {	
	
	private long request_id;
	private boolean response_expected;
	private String object_key;
	private String operation;
	private String[] types;
	
	public long getRequestId() {
		return request_id;
	}

	public void setRequestId(long request_id) {
		this.request_id = request_id;
	}

	public boolean isResponseExpected() {
		return response_expected;
	}

	public void setResponseExpected(boolean response_expected) {
		this.response_expected = response_expected;
	}

	public String getObjectKey() {
		return object_key;
	}

	public void setObjectKey(String object_key) {
		this.object_key = object_key;
	}

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public String[] getTypes() {
		return types;
	}

	public void setTypes(String[] types) {
		this.types = types;
	}

	public void write(OutputStream out) throws IOException {
		DataOutputStream dos = new DataOutputStream(out);
		dos.writeLong(request_id);
		dos.writeBoolean(response_expected);
		dos.writeUTF(object_key);
		dos.writeUTF(operation);
		dos.write(types.length);
		for (String type : types) {
			dos.writeUTF(type);
		}
	}
	
	public static RequestHeader read(InputStream in) throws IOException {
		DataInputStream dis = new DataInputStream(in);
		RequestHeader result = new RequestHeader();
		result.setRequestId(dis.readLong());
		result.setResponseExpected(dis.readBoolean());
		result.setObjectKey(dis.readUTF());
		result.setOperation(dis.readUTF());
		
		int typesLength = dis.read();
		String[] types = new String[typesLength];		
		for (int i = 0; i < typesLength; i++) {
			types[i] = dis.readUTF();
		}
		result.setTypes(types);
		
		return result;
	}
}
